package com.github.io.controller;

import com.github.io.entity.User;
import com.github.io.service.IUserService;
import com.github.io.utils.ResponseResult;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ruiyanglee
 * @date: 2020-09-09 09:59
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    public static final Logger log = LoggerFactory.getLogger(UserController.class);


    @Autowired
    private IUserService userService;

    @GetMapping
    public ResponseResult list(HttpServletRequest request) {
        String id = request.getSession().getId();
        System.out.println("user session id:"+ id);
        try {
            List<User> users = userService.list();
            return ResponseResult.ok().message("请求成功").data(users);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseResult.error().message("请求失败").code(404);
    }

    @GetMapping("/{id}")
    public ResponseResult userInfo(@PathVariable String id ) {
        try {
            User user = userService.getUserById(id);
            return ResponseResult.ok().message("请求成功").data(user);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseResult.error().message("请求失败").code(404);
    }
    
    
    @GetMapping("/session")
    public Object getSession(HttpServletRequest request){
        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession();
        String id = session.getId();
        long creationTime = session.getCreationTime();
        long lastAccessedTime = session.getLastAccessedTime();
        
        result.put("sessionId", id);
        result.put("creationTime", creationTime);
        result.put("lastAccessedTime", lastAccessedTime);
        
        return result;
    }


//    @DeleteMapping("/{id}")
//    public ResponseResult deleteById(@PathVariable String id) {
//        try {
//            return ResponseResult.ok().message("删除成功");
//        } catch (Exception e) {
//            log.error(e.getMessage());
//        }
//        return ResponseResult.error().message("删除失败，用户不存在").code(404);
//    }
//
//    @PutMapping("")
//    public ResponseResult pageTeacherCondition(@RequestBody UserVO userVO) {
//        try {
//            return ResponseResult.ok().message("请求成功").data(null);
//        } catch (Exception e) {
//            log.error(e.getMessage());
//        }
//        return ResponseResult.error().message("请求失败").code(404);
//    }
//
//    @PostMapping("/add")
//    public ResponseResult saveTeacher(@RequestBody User user) {
//        try {
//            userService.save(user);
//            return ResponseResult.ok().message("更新成功");
//        } catch (Exception e) {
//            log.error(e.getMessage());
//        }
//        return ResponseResult.error().message("更新失败").code(404);
//    }
}
